<?php

/**
 * Created by PhpStorm.
 * User: feng
 * Date: 2016/12/19
 * Time: 上午11:16
 */
class Lyw_user_model extends CI_Model
{
    public function __construct()
    {
        $this->load->database();
    }

    /**
     * 用户账号密码认证
     */
    public function attestation($username, $password)
    {
        $sql = 'select * from lyw_user where username = ?';
        $row = $this->db->query($sql, array($username))->row();
        if (!empty($row)) {
            if ($row->is_stop == 1) {
                return array('code' => 5);
            }
            $buildpassword = buildPassword($password, $row->rand_str);
            if ($buildpassword == $row->password) {
                return array('code' => 1, 'user_id' => $row->id, 'user_type' => $row->type, 'username' => $row->username, 'header_image' => $row->header_image);
            } else {
                return array('code' => 2);
            }
        } else {
            return array('code' => 3);
        }
    }

    public function hadUsername($username)
    {
        $sql = 'select count(*) as count from lyw_user where username = ?';
        $row = $this->db->query($sql, array($username))->row();
        if ($row->count == 0) {
            return false;
        } else {
            return true;
        }
    }

    public function add($username, $password, $invitecode, $type, $email, $tel)
    {
        $rand_str = getRandStr(8);
        $new_password = buildPassword($password, $rand_str);
        $this->db->trans_start();
        $sql = 'insert into lyw_user (username,password,email,tel,type,rand_str,creat_time) values (?,?,?,?,?,?,?)';
        $this->db->query($sql, array($username, $new_password, $email, $tel, $type, $rand_str, time()));
        $user_id = $this->db->insert_id();
        $this->load->model('lyw_invite_code_manager_model');
        if ($this->lyw_invite_code_manager_model->useCode($user_id, $invitecode)) {
            $arr = array('code' => 1, 'user_id' => $user_id);
        } else {
            $arr = array('code' => 0, 'user_id' => 0);
        }
        $this->db->trans_complete();
        return $arr;
    }

    public function cPassword($username, $password)
    {
        $sql = 'select * from lyw_user where username = ?';
        $row = $this->db->query($sql, array($username))->row();
        if (empty($row)) {
            return false;
        }
        $new_password = buildPassword($password, $row->rand_str);
        $sql = 'update lyw_user set password = ? where id = ?';
        $this->db->query($sql, array($new_password, $row->id));
        if ($this->db->affected_rows() == 1) {
            return true;
        } else {
            return false;
        }
    }

    public function getSearchUserlist($page, $rows, $name = '')
    {
        if ($page < 1) {
            $limit_begin = 0;
        } else {
            $limit_begin = ($page - 1) * $rows;
        }
        $limit_end = $rows;
        if (!empty($name)) {
            $sql = "select * from lyw_user where (type = 1 or type =2)and name like '%{$name}%' limit {$limit_begin} , $limit_end";
        } else {
            $sql = "select * from lyw_user where (type = 1 or type =2) limit {$limit_begin} , $limit_end";
        }
        $result = $this->db->query($sql)->result_array();
        return $result;
    }

    public function searchCount($name)
    {
        if (empty($name)) {
            $sql = "select count(*) as count from lyw_user";
        } else {
            $sql = "select count(*) as count from lyw_user where name like '%{$name}%'";
        }
        $row = $this->db->query($sql)->row();
        return $row->count;
    }

    public function stopUser($user_id)
    {
        $sql = 'update lyw_user set is_stop = 1 where id = ?';
        $this->db->query($sql, array($user_id));
        if ($this->db->affected_rows() == 1) {
            return true;
        } else {
            return false;
        }
    }

    public function startUser($user_id)
    {
        $sql = 'update lyw_user set is_stop = 0 where id = ?';
        $this->db->query($sql, array($user_id));
        if ($this->db->affected_rows() == 1) {
            return true;
        } else {
            return false;
        }
    }

    public function updateUserByUserCenter($id, $name, $school, $address_life, $introduce, $professional, $header_img = '')
    {
        if (empty($header_img)) {
            $data = array('name' => $name, 'school' => $school, 'address_life' => $address_life, 'introduce' => $introduce, 'professional' => $professional);
        } else {
            $data = array('name' => $name, 'school' => $school, 'address_life' => $address_life, 'introduce' => $introduce, 'professional' => $professional, 'header_image' => $header_img);
        }
        $where = "id = {$id}";
        $str = $this->db->update_string('lyw_user', $data, $where);
        $this->db->query($str);
        $affected_rows = $this->db->affected_rows();
        if (isset($affected_rows) && $affected_rows == 1) {
            return true;
        } else {
            return false;
        }
    }

    public function updateHeaderImage($id, $header_img = '')
    {
        $sql = 'update lyw_user set header_image = ? where id = ?';
        $this->db->query($sql, array($header_img, $id));
        return true;
    }

    public function getUserInfor($id)
    {
        $sql = "select * from lyw_user where id = {$id}";
        $list = $this->db->query($sql)->result_array();
        return $list[0];
    }

    public function upUser($user_id)
    {
        if ($this->lyw_common_model->userUpPermission($user_id)) {
            $sql = 'update lyw_user set type = 2 where id = ?';
            $this->db->query($sql, array($user_id));
            return true;
        } else {
            return false;
        }
    }

    public function updateJingYan($user_id, $is_add, $jingyan, $jingyan_sum)
    {
        if ($is_add == 1) {
            $jingyan_sum = $jingyan_sum + $jingyan;
        } elseif ($is_add == 2) {
            $jingyan_sum = $jingyan_sum - $jingyan;
            if ($jingyan_sum < 0) {
                $jingyan_sum = 0;
            }
        }
        $level_arr = calculateLevel($jingyan_sum);
        $sql = "update lyw_user set jingyan = {$jingyan_sum} ,level = {$level_arr['level']} where id = {$user_id}";
        $this->db->query($sql);
        return true;
    }

    public function updateGaoFei($user_id, $gaofei)
    {
        $sql = "update lyw_user set money = money + ? where id = ?";
        $this->db->query($sql, array($gaofei, $user_id));
        return true;
    }

    public function updateZhiFuBao($user_id, $zhifubao_account)
    {
        $sql = "update lyw_user set zhifubao_account = ? where id = ?";
        $this->db->query($sql, array($zhifubao_account, $user_id));
        return true;
    }

    public function tixian($user_id)
    {
        //提现即清空账户内的大洋
        $sql = "update lyw_user set money = 0 where id = ?";
        $this->db->query($sql, array($user_id));
        return true;
    }

    public function punishment($user_id)
    {
        $sql = "update lyw_user set receive_time = ? where id = ?";
        $this->db->query($sql, array((time() + 60 * 60 * 24), $user_id));
        return true;
    }

    public function getUserInforByUsername($username)
    {
        $sql = "select * from lyw_user where username = ?";
        $list = $this->db->query($sql , array($username))->result_array();
        return $list;
    }

}
